﻿#include "planadd.h"
#include "ui_planadd.h"

PlanAdd::PlanAdd(QWidget *parent) :
    ItemAdd(parent),
    ui(new Ui::PlanAdd)
{
    ui->setupUi(this);

    table=PLAN_EPIBOLY_INDEX;
    detailTable=PLAN_EPIBOLY_DETAIL;
    saveOk=false;

    _uid=QUuid::createUuid().toString();
    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
}

PlanAdd::~PlanAdd()
{
    delete ui;
}

void PlanAdd::updateView(const QString &uid)
{
    model->setTable(detailTable);
    model->setFilter(QString("uid='%1'").arg(uid));
    model->select();

    ui->tableView->hideColumn(0);       //id
    ui->tableView->hideColumn(1);       //uid
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("名称"));    //name
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("数量"));    //number
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("要求"));    //request
    ui->tableView->hideColumn(5);   // status
    model->setHeaderData(6,Qt::Horizontal,QStringLiteral("备注"));    //remark
}

bool PlanAdd::saveIndex(const QString &customer, const QString &period, const QString &remark)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (uid,date,customer,period,host,status,remark) values (?,?,?,?,?,?,?)").arg(table));
    query.addBindValue(_uid);
    query.addBindValue(Env::today);
    query.addBindValue(customer);
    query.addBindValue(period);
    query.addBindValue(Env::hostName);
    query.addBindValue(status);
    query.addBindValue(remark);

    return query.exec();
}

bool PlanAdd::saveDetail(const QString &name, const QString &number, const QString &request, const QString &remark)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (uid,name,number,request,status,remark) values (?,?,?,?,?,?)").arg(detailTable));
    query.addBindValue(_uid);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(request);
    query.addBindValue(status);
    query.addBindValue(remark);

    return query.exec();
}

void PlanAdd::on_btn_submit_clicked()
{
    if(ui->contactPan->isEnabled())
    {
        QString customer=ui->customer->text().trimmed();
        if(customer.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("客户姓名不能为空！"));
            return;
        }

        QString period=ui->period->text().trimmed();
        QString remark=ui->remark->text().trimmed();
        if(!saveIndex(customer,period,remark))
        {
            QMessageBox::warning(this,QStringLiteral("数据库异常"),QStringLiteral("无法保存客户信息！"));
            return;
        }
        pair.first=PAIR_INDEX;

        QString name=ui->name->text().trimmed();
        QString number=ui->number->text().trimmed();
        if(name.isEmpty() || number.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"));
            return;
        }

        QString request=ui->request->text().trimmed();
        QString item_remark=ui->item_remak->text().trimmed();
        if(saveDetail(name,number,request,item_remark))
        {
            updateView(_uid);
            ui->name->clear();
            ui->number->clear();
            ui->request->clear();

            pair.second=PAIR_DETAIL;
            saveOk=true;
            if(pair.first == PAIR_INDEX && pair.second== PAIR_DETAIL)
                ui->contactPan->setEnabled(false);
        }
        else
            QMessageBox::critical(this,QStringLiteral("异常情况"),QStringLiteral("异常情况，提交失败！"));
    }
    else
    {
        QString name=ui->name->text().trimmed();
        QString number=ui->number->text().trimmed();
        if(name.isEmpty() || number.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"));
            return;
        }

        QString request=ui->request->text().trimmed();
        QString item_remark=ui->item_remak->text().trimmed();
        if(saveDetail(name,number,request,item_remark))
        {
            updateView(_uid);
            ui->name->clear();
            ui->number->clear();
            ui->request->clear();
        }
        else
            QMessageBox::critical(this,QStringLiteral("异常情况"),QStringLiteral("异常情况，提交失败！"));
    }
}

void PlanAdd::on_btn_save_clicked()
{
    model->submitAll();
}
